<template>
  <div class="personal-center">
    <div class="personal-card">
      <div class="avatar-section">
        <img class="avatar" :src="userAvatar" alt="avatar" />
        <div class="username">{{ user.name }}</div>
        <div class="role-tag">{{ user.role === 'ROLE_ADMIN' ? '管理员' : '普通用户' }}</div>
      </div>
      <el-divider></el-divider>
      <div class="info-section">
        <div class="info-row">
          <span class="info-label">手机号：</span>
          <span class="info-value">{{ user.phone || '未填写' }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">年龄：</span>
          <span class="info-value">{{ user.age || '未填写' }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">性别：</span>
          <span class="info-value">{{ user.sex || '未填写' }}</span>
        </div>
      </div>
      <el-divider></el-divider>
      <div class="edit-section">
        <h3>修改个人信息</h3>
        <el-form :model="form" label-width="60px" class="edit-form">
          <el-form-item label="用户名">
            <el-input v-model="form.name" placeholder="请输入新用户名"></el-input>
            <el-button type="primary" size="mini" @click="updateName" style="margin-left:10px;">修改</el-button>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" type="password" placeholder="请输入新密码"></el-input>
            <el-button type="primary" size="mini" @click="updatePassword" style="margin-left:10px;">修改</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="action-section">
        <el-button @click="$router.push('/')" type="warning" plain>返回首页</el-button>
        <el-button @click="$router.push('/login')" type="danger" plain>退出登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {},
      form: {
        name: '',
        password: ''
      }
    }
  },
  computed: {
    userAvatar() {
      // 可自定义头像逻辑
      return 'https://api.dicebear.com/7.x/miniavs/svg?seed=' + (this.user.name || 'user');
    }
  },
  methods: {
    updateName() {
      if (!this.form.name) {
        this.$message.warning("请输入新用户名");
        return;
      }
      request.post('/admin/update', { ...this.user, name: this.form.name }).then(res => {
        if (res.code === '0') {
          this.$message.success("用户名修改成功");
          this.user.name = this.form.name;
          localStorage.setItem('user', JSON.stringify(this.user));
          this.form.name = '';
        } else {
          this.$message.error(res.msg || "修改失败");
        }
      });
    },
    updatePassword() {
      if (!this.form.password) {
        this.$message.warning("请输入新密码");
        return;
      }
      request.post('/admin/update', { ...this.user, password: this.form.password }).then(res => {
        if (res.code === '0') {
          this.$message.success("密码修改成功");
          this.form.password = '';
        } else {
          this.$message.error(res.msg || "修改失败");
        }
      });
    }
  }
}
</script>

<style scoped>
.personal-center {
  min-height: 100vh;
  background: linear-gradient(135deg, #e3eafc 60%, #f5f7fa 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.personal-card {
  width: 420px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.10);
  padding: 36px 32px 24px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 10px;
}
.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-bottom: 12px;
  background: #f5f7fa;
  border: 2px solid #409EFF;
}
.username {
  font-size: 22px;
  font-weight: bold;
  color: #222;
  margin-bottom: 4px;
}
.role-tag {
  font-size: 13px;
  color: #fff;
  background: #409EFF;
  border-radius: 10px;
  padding: 2px 12px;
  margin-bottom: 8px;
}
.info-section {
  width: 100%;
  margin-bottom: 10px;
}
.info-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 15px;
}
.info-label {
  color: #888;
}
.info-value {
  color: #333;
  font-weight: 500;
}
.edit-section {
  width: 100%;
  margin: 18px 0 10px 0;
}
.edit-form {
  width: 100%;
}
.action-section {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 18px;
}
</style>